﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WPFDataPageDemo 
{
    internal class MainWindowViewModel : ObservableObject
    {
        public MainWindowViewModel()
        {
            // 初始化数据源，这部分是Model层数据
            this.EmployeeList = new List<Employee>();
            for (int i = 0; i < 100; i++)
            {
                this.EmployeeList.Add(new Employee() { 
                    Name = "Name" + i.ToString(),
                    Age = i,
                    PhoneNumber = string.Format("110{0}", i)
                });
            }

            // 初始化每页记录数选项
            this.PageCountList = new List<int> { 10, 20, 50, 100 };
            // 总记录数
            this.TotalCount = EmployeeList.Count;
            // 当前页
            this.CurrentPage = 1;
            // 初始化页
            this.PageList = new List<Page>();
            // 计算总页数
            int pageCount = this.TotalCount / this.PageCountList[CurrentIndex] +
                ((this.TotalCount % this.PageCountList[CurrentIndex]) == 0 ? 0 : 1);
            // 
            if (pageCount <= 7)
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    this.PageList.Add(new Page() { Name = i.ToString() });
                }
            }
            else if (pageCount > 7)
            {
                this.PageList.Add(new Page() { Name = "1" });
                this.PageList.Add(new Page() { Name = "2" });
                this.PageList.Add(new Page() { Name = "3" });
                this.PageList.Add(new Page() { Name = "..." });
                this.PageList.Add(new Page() { Name = (pageCount - 2).ToString() });
                this.PageList.Add(new Page() { Name = (pageCount - 1).ToString() });
                this.PageList.Add(new Page() { Name = pageCount.ToString() });
            }

            // 分页
            Pager();
        }

        /// <summary>
        /// 分页函数
        /// </summary>
        private void Pager()
        {
            // 当前页的起始记录位置
            this.StartPos = (this.CurrentPage - 1) * this.PageCountList[this.CurrentIndex] + 1;
            // 当前页的结束记录位置
            this.EndPos = this.CurrentPage * this.PageCountList[this.CurrentIndex];
            // 修改前台绑定列表的数据
            this.EmployeeBindingList = this.EmployeeList.Take(this.CurrentPage * this.PageCountList[this.CurrentIndex]).
                Skip((this.CurrentPage - 1) * this.PageCountList[this.CurrentIndex]).ToList();
        }

        private RelayCommand _lastPageCommand;
        public RelayCommand LastPageCommand
        {
            get
            {
                if (_lastPageCommand == null)
                {
                    _lastPageCommand = new RelayCommand(() =>
                    {
                        this.CurrentPage--;
                        Pager();
                    });
                }
                return _lastPageCommand;
            }
        }

        private RelayCommand _nextPageCommand;
        public RelayCommand NextPageCommand
        {
            get
            {
                if (_nextPageCommand == null)
                {
                    _nextPageCommand = new RelayCommand(() =>
                    {
                        this.CurrentPage++;
                        Pager();
                    });
                }
                return _nextPageCommand;
            }
        }

        private RelayCommand _gotoPageCommand;
        public RelayCommand GotoPageCommand
        {
            get
            {
                if (_gotoPageCommand == null)
                {
                    _gotoPageCommand = new RelayCommand(() =>
                     {
                         Pager();
                     });
                }
                return _gotoPageCommand;
            }
        }

        #region NotifyProperty
        private int _currentIndex;
        public int CurrentIndex
        {
            get => _currentIndex;
            set => SetProperty(ref _currentIndex, value);
        }

        private List<Employee> _employeeList;
        public List<Employee> EmployeeList
        {
            get => _employeeList;
            set => SetProperty(ref _employeeList, value);
        }

        private List<Employee> _employeeBindingList;
        public List<Employee> EmployeeBindingList
        {
            get => _employeeBindingList;
            set => SetProperty(ref _employeeBindingList, value);
        }

        private List<Page> _pageList;
        public List<Page> PageList
        {
            get => _pageList;
            set => SetProperty(ref _pageList, value);
        }

        private List<int> _pageCountList;
        public List <int> PageCountList
        {
            get => _pageCountList;
            set => SetProperty(ref _pageCountList, value);
        }

        private int _startPos;
        public int StartPos
        {
            get => _startPos;
            set => SetProperty(ref _startPos, value);
        }

        private int _endPos;
        public int EndPos
        {
            get => _endPos;
            set => SetProperty(ref _endPos, value);
        }

        private int _totalCount;
        public int TotalCount
        {
            get => _totalCount;
            set => SetProperty(ref _totalCount, value);
        }
        private int _currentPage;
        public int CurrentPage
        {
            get => _currentPage;
            set => SetProperty(ref _currentPage, value);
        }
#endregion
    }
    public class Page
    {
        public string Name { get; set; }
    }
}
